<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :inline="true">
      <el-form-item>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cms:db-backup:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="数据源名称" align="center" prop="connectName" />
      <el-table-column label="IP" align="center" prop="host" />
      <el-table-column label="库名" align="center" prop="dbName" />
      <el-table-column label="备份文件数" align="center" prop="fileCount" />
      <el-table-column label="定时备份" align="center" prop="cron" >
        <template #default="scope">
          <el-button v-if="scope.row.cron"
            link
            type="primary"
            @click="openTaskForm(scope.row.id)"
            v-hasPermi="['cms:db-backup:backup']"
          >
          {{ scope.row.cron }}
          </el-button>
          <el-button v-else
            link
            type="primary"
            @click="openTaskForm(scope.row.id)"
            v-hasPermi="['cms:db-backup:backup']"
          >
            +定时备份
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleBackup(scope.row.id)"
            v-hasPermi="['cms:db-backup:backup']"
          >
            备份
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms:db-backup:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:db-backup:delete']"
          >
            删除
          </el-button>
          <el-button
            link
            type="danger"
            @click="openDbfileDialog(scope.row.dbType,scope.row.id)"
            v-hasPermi="['cms:db-backup:query']"
          >
            备份文件
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DataSourceConfigForm ref="formRef" @success="getList" />
  <!-- 表单弹窗：备份文件列表 -->
  <DbFilesDialog ref="dbFileRef" @success="getList" />
  <!-- 表单弹窗：定时任务 -->
  <DbBackupTaskForm ref="taskFormRef" @success="getList" />
</template>
<script lang="ts" setup>
import * as DataBackupApi from '@/api/infra/dbbackup'
import DataSourceConfigForm from './DataSourceConfigForm.vue'
import DbBackupTaskForm from './DbBackupTaskForm.vue'
import DbFilesDialog from './DbFiles.vue'
defineOptions({ name: 'InfraDataSourceConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    list.value = await DataBackupApi.getDataSourceConfigList()
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
/** 备份文件操作 */
const dbFileRef = ref()
const openDbfileDialog = (dbType:string,id: number) => {
  dbFileRef.value.open(dbType,id)
}
/** 定时任务弹窗操作 */
const taskFormRef = ref()
const openTaskForm = (id: number) => {
  taskFormRef.value.open(id)
}
/** 备份按钮操作 */
const handleBackup = async (id: number) => {
  try {
    // 备份的二次确认
    await message.confirm('确认备份吗?')
    // 发起备份
    await DataBackupApi.dbBackup(id)
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DataBackupApi.deleteDataSourceConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
